import Dialog from "../../../component/vant/dialog/dialog";

let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        instoreId: "",
        instore: {},
        dynamicCargoData: [],

        //action-sheet
        actions: [
            {name: '新增', type: "add"},
            {name: '复制', type: "copy"},
            {name: '撤销', type: "cancel"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let obj = wx.getLaunchOptionsSync()
        if (obj) {
            // instoreNo = obj.query.instoreNo
        }

        let instoreId = options.instoreId
        this.data.instoreId = instoreId
        if (instoreId && instoreId != null) {
            this.queryData(instoreId)
        }

    },

    queryData: function (instoreId) {
        let _this = this
        this.setData({
            spinShow: true
        });
        app.formGet('/app/instore/getInstoreDetail?instoreId=' + instoreId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    instore: res.data.instore,
                    dynamicCargoData: res.data.detailList
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    handleInstoreEdit: function (){
        wx.navigateTo({
            url: "/pages/instore/edit/instoreEdit?instoreId=" + this.data.instoreId
        })
    },
    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/instore/add/index"
            })
        } else if (type == 'copy') {
            wx.redirectTo({
                url: "/pages/instore/add/index?copyInstoreId=" + this.data.instoreId
            })
        } else if (type == 'cancel') {
            Dialog.confirm({
                title: '是否确认撤销该单据？',
                message: '撤销后将撤销相应的库存、账目，且撤销后不可恢复',
            })
                .then(() => {
                    this.handleInstoreCancel()
                })
                .catch(() => {
                    console.log("concel")
                });
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    handleInstoreCancel() {
        let _this = this
        let data = {}
        data.instoreId = this.data.instoreId
        app.formPost('/app/instore/cancel', data).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                app.message(res.message, 'success')
                setTimeout(function () {
                    wx.redirectTo({
                        url: '/pages/instore/index/index'
                    })
                }, 2000)
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

})
